﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if ("".Equals(textBox1.Text))
            {
                MessageBox.Show("请输入sharedsecret!");
                return;
            }
            button1.Enabled = false;
            textBox1.Enabled = false;
            getCode();
            
        }
        private void getCode()
        {
            setCode();
            System.Timers.Timer t = new System.Timers.Timer(1000*3);
            t.Enabled = true;
            t.AutoReset = true;
            t.Elapsed += (s, e) =>
            {
                setCode();
            };
            t.Start();
        }
        private delegate void MyDelegateUI();
        private void setCode()
        {
            long time = DateTimeUtil.DateTimeToLongTimeStamp(DateTime.Now) / 1000;
            try
            {
                MyDelegateUI d = delegate
                {
                    label1.Text = SteamCodeGenerator.generateSteamGuardCode(time, textBox1.Text);
                };
                label1.Invoke(d);
            }
            catch (Exception)
            {
                MyDelegateUI d = delegate
                {
                    label1.Text = "未获取到";
                };
                label1.Invoke(d);
            }
            
        }
    }
}
